package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Network;
import com.example.demo.entity.Product;
import com.example.demo.mapper.NetworkMapper;
import com.example.demo.mapper.ProductMappper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 ** Create by weiweiweiweiwei on 2021/8/8
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductMappper productMappper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Product product) {
        productMappper.insert(product);
        return Result.success();
    }

    @DeleteMapping("/{rank_num}") //删
    public Result<?> delete(@PathVariable long rank_num) {
        productMappper.deleteById(rank_num);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {
        LambdaQueryWrapper<Product> wrapper = Wrappers.<Product>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Product::getRankNum,search);
        }
        Page<Product> NewsPage = productMappper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(NewsPage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody Product product) {
        productMappper.updateById(product);
        return Result.success();
    }
}
